<?php

/**
 * adminLeg actions.
 *
 * @package    sf_sandbox
 * @subpackage adminLeg
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminLegActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->aoi_legs = Doctrine_Core::getTable('AoiLeg')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AoiLegForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AoiLegForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($aoi_leg = Doctrine_Core::getTable('AoiLeg')->find(array($request->getParameter('id'))), sprintf('Object aoi_leg does not exist (%s).', $request->getParameter('id')));
    $this->form = new AoiLegForm($aoi_leg);
    $this->leg = $aoi_leg;
    $this->listPassengers($aoi_leg, $request);
  }


  public function executeExport(sfWebRequest $request)
  {
    $this->forward404Unless($aoi_leg = Doctrine_Core::getTable('AoiLeg')->find(array($request->getParameter('id'))), sprintf('Object aoi_leg does not exist (%s).', $request->getParameter('id')));
    $this->listPassengers($aoi_leg, $request);
    $this->leg = $aoi_leg;
    $this->setLayout(false);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($aoi_leg = Doctrine_Core::getTable('AoiLeg')->find(array($request->getParameter('id'))), sprintf('Object aoi_leg does not exist (%s).', $request->getParameter('id')));
    $this->form = new AoiLegForm($aoi_leg);
    $this->leg = $aoi_leg;

    $this->processForm($request, $this->form);
    $this->listPassengers($aoi_leg, $request);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($aoi_leg = Doctrine_Core::getTable('AoiLeg')->find(array($request->getParameter('id'))), sprintf('Object aoi_leg does not exist (%s).', $request->getParameter('id')));
    $aoi_leg->delete();

    $this->redirect('adminLeg/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $aoi_leg = $form->save();

      $this->redirect('adminLeg/edit?id='.$aoi_leg->getId());
    }
  }

  protected function listPassengers($leg, $request)
  {
    $this->passenger_status = $request->getParameter('passenger_status');

    $sql = 'SELECT P.*, R.ref_code as reservation_ref , R.status as reservation_status, R.contact_email, R.contact_phone, R.amount FROM aoi_passenger P '.
           'INNER JOIN aoi_itinerary_reservation R ON R.id = P.reservation_id '.
           'INNER JOIN aoi_itinerary_reservation_leg RL ON RL.reservation_id = R.id '.
           ' WHERE RL.leg_id = '.$leg->getId().' ';

    if(in_array($this->passenger_status, array('created','pending','validated','cancelled'))) $sql .= ' AND R.status = \''. $this->passenger_status.'\' ';

    $sql .= 'ORDER BY R.ref_code, P.lastname';

    $q = Doctrine_Manager::getInstance()->connection();
    $this->passengers = $q->execute($sql);
  }
}
